@echo off
SETLOCAL

:: Set path variables
SET BACKTRADER_PATH=./backtrader
SET BUILD_DIR=build
SET EGG_INFO_DIR=backtrader.egg-info
SET BENCHMARKS_DIR=.benchmarks

:: Install dependencies from requirements.txt
echo Installing dependencies from requirements.txt...
pip install -U -r requirements.txt
IF %ERRORLEVEL% NEQ 0 (
    echo Failed to install dependencies. Please check the requirements.txt file.
    exit /b 1
)

:: Switch to the parent directory
echo Switching to the parent directory...
cd ..
IF %ERRORLEVEL% NEQ 0 (
    echo Failed to switch directory.
    exit /b 1
)

:: Install the backtrader package
echo Installing the backtrader package...
pip install -U --no-build-isolation  %BACKTRADER_PATH%
IF %ERRORLEVEL% NEQ 0 (
    echo Failed to install the backtrader package.
    exit /b 1
)

:: Delete intermediate build and egg-info directories
echo Deleting intermediate files...
cd backtrader
IF EXIST %BUILD_DIR% (
    rmdir /s /q %BUILD_DIR%
    echo Deleted %BUILD_DIR% directory.
)
IF EXIST %EGG_INFO_DIR% (
    rmdir /s /q %EGG_INFO_DIR%
    echo Deleted %EGG_INFO_DIR% directory.
)

:: Run backtrader tests with 4 parallel processes
echo Running backtrader tests...
cd ..
pytest %BACKTRADER_PATH%/tests -n 4
IF %ERRORLEVEL% NEQ 0 (
    echo Test cases failed.
    exit /b 1
)

:: Delete the .benchmarks directory generated by pytest
IF EXIST %BENCHMARKS_DIR% (
    rmdir /s /q %BENCHMARKS_DIR%
    echo Deleted %BENCHMARKS_DIR% directory.
)
cd ./backtrader
:: Script completed
echo Script execution completed!

:: Pause to view the output
pause